package com.monitor.log.biz.core;

import org.slf4j.MDC;

import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;

public class BizLogSpanIdUtil {

	private static final String SPAN_ID = "spanId";
    /**
     * spanId为空时，显示的spanId。
     */
    private static final String DEFAULT_SPAN_ID = "0";

    /**
     * 设置spanId
     */
    public static void setSpanId(String spanId) {
        //如果参数为空，则设置默认spanId
    	spanId = FrameStringUtil.isEmpty(spanId) ? DEFAULT_SPAN_ID : spanId;
        //将traceId放到MDC中
        MDC.put(SPAN_ID, spanId);
    }

    /**
     * 获取traceId
     */
    public static String getSpanId() {
        //获取
        String spanId = MDC.get(SPAN_ID);
        //spanId为空，则返回默认值
        return FrameStringUtil.isEmpty(spanId) ? DEFAULT_SPAN_ID : spanId;
    }

    /**
     * 判断spanId为默认值
     */
    public static Boolean defaultTraceId(String spanId) {
        return DEFAULT_SPAN_ID.equals(spanId);
    }

    /**
     * 生成spanId
     */
    public static String genSpanId() {
        return FrameNoUtil.uuid();
    }
}